<?php
if (file_exists ( dirname ( __FILE__ ) . '/ObjectAccess.class.php' )) {
	require_once dirname ( __FILE__ ) . '/ObjectAccess.class.php';
}

/**
 * Simple class that uses predefined Word XML templates to generate a document
 * @author andrei.nemeti <ane@auximus.com>
 */
class WordXmlDocument extends ObjectAccess {
	
	/**
	 * The directory where the Word XML template resides
	 * @var string
	 */
	protected $_templateDir = NULL;
	
	/**
	 * The current document
	 * @var WordXmlDocumentElement
	 */
	protected $_doc = NULL;
	
	public function __construct() {
		if (file_exists ( dirname ( __FILE__ ) . '/WordXmlDocumentElement.class.php' )) {
			require_once dirname ( __FILE__ ) . '/WordXmlDocumentElement.class.php';
		}
		if (! class_exists ( 'WordXmlDocumentElement' )) {
			throw new Exception ( 'Class WordXmlDocumentElement not found!' );
			return false;
		}
	}
	
	/**
	 * Set the path to the template you want to use
	 * @param string $templateDir
	 * @return boolean
	 * @throws Exception
	 */
	public function setTemplateDirectory($templateDir = NULL) {
		if (is_null ( $templateDir )) {
			throw new Exception ( 'Template path is required' );
			return false;
		}
		
		if (! file_exists ( $templateDir ) || ! is_dir ( $templateDir )) {
			throw new Exception ( 'Template directory <i>' . $templateDir . '</i> does not exist' );
			return false;
		}
		
		$this->_templateDir = $templateDir;
		return true;
	}
	
	/**
	 * Load an image and return it as a base64 encoded string so it can be included in the document
	 * @param string $imagePath Path to image
	 * @return string base64 encoded image
	 * @throws Exception
	 */
	public function image($imagePath = NULL) {
		if (is_null ( $imagePath )) {
			throw new Exception ( 'Image path is required' );
			return false;
		}
		if (! file_exists ( $imagePath ) || is_dir ( $imagePath )) {
			throw new Exception ( '<i>' . $imagePath . '</i> is not a file' );
			return false;
		}
		$image = file_get_contents ( $imagePath );
		return base64_encode ( $image );
	}
	
	/**
	 * Output the generated Word XML document as attachement for download pourposes
	 * @param string $filename
	 */
	public function output($filename = NULL) {
		header ( "Content-Type: application/xml; charset=UTF-8" );
		header ( "Expires: 0" );
		header ( "Cache-Control: must-revalidate, post-check=0, pre-check=0" );
		if (! is_null ( $filename )) {
			$filename = $filename . date ( 'dmY-his' ) . ".xml";
			header ( "content-disposition: attachment;filename=$filename" );
		}
		return $this->_doc->output ();
	}
	
	/**
	 * Create a new WordXmlDocumentElement and add it as a child of current one (or root _doc if empty)
	 * @param string $elementName
	 * @param unknown_type $contents
	 * @throws Exception
	 * @return mixed False on failure, WordXmlDocumentElement on success
	 */
	public function createElement($elementName = NULL, $contents = NULL) {
		if (is_null ( $elementName )) {
			throw new Exception ( 'Element name is required in <i>' . __CLASS__ . '</i> method <i>' . __FUNCTION__ . '()</i>' );
			return false;
		}
		/*
		if (isset ( $this->$elementName )) {
			throw new Exception ( "Element <i>$elementName</i> already exists" );
			return false;
		}
		*/
		$this->$elementName = new WordXmlDocumentElement ( $elementName, $this->_templateDir );
		$this->$elementName->load ( $elementName, $contents );
		$this->_doc = $this->$elementName;
		
		return $this->_doc;
	}
	
	/**
	 * Remove an existing element
	 * @param string $elementName
	 * @throws Exception
	 * @todo Implement and test
	 */
	public function removeElement($elementName = NULL) {
		if (is_null ( $elementName )) {
			throw new Exception ( 'Element name is required in <i>' . __FUNCTION__ . '</i>' );
			return false;
		}
		
		if (isset ( $this->$elementName )) {
			unset ( $this->$elementName );
		}
		return true;
	}
	
	public function __destruct() {
	
	}
}